/*
    RExLib project, file drimysql.h
    Copyright (C) 2012  Sarvaritdinov Ravil ra9oaj@gmail.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#ifndef DRIMYSQL_H
#define DRIMYSQL_H

#if defined (_MSC_VER)
#ifdef __cplusplus
#define LIB_EXPORT extern "C" __declspec(dllexport)
#else
#define LIB_EXPORT __declspec(dllexport)
#endif
#elif defined (__GNUC__)
#ifdef __cplusplus
#define LIB_EXPORT extern "C" __attribute__((visibility("default")))
#else
#define LIB_EXPORT __attribute__((visibility("default")))
#endif
#else
#warning Unknown dynamic link semantics
#ifdef __cplusplus
#define LIB_EXPORT extern "C"
#else
#define LIB_EXPORT
#endif
#endif

#include <mysql/mysql.h>
#include <malloc.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <dbconnectorprivate.h>

using namespace rex;

class drimysql : public DBconnectorPrivate
{

public:
  virtual int errorNo() const;
  virtual const char* nativeErrorString() const;
  virtual int nativeErrorNo() const;
  virtual bool seek ( long long unsigned int pos );
  virtual bool first();
  virtual bool next();
  virtual int query ( const char* qry );
  virtual unsigned int fieldCount() const;
  virtual long long unsigned int affectedRows() const;
  virtual long long unsigned int rowCount() const;
  virtual char* value ( int val );
  virtual int dbState() const;
  virtual bool closeDB();
  virtual bool openDB ( const char* host, uint port = 0 );
  virtual void setUnixConnection ( bool unixf = true );
  virtual void setSSLConnection ( bool sslf = true );
  virtual void setDBUser ( const char* username, const char* passw = 0 );
  virtual void setDBName ( const char* name );
  virtual int errorCode() const;
  virtual const char* errorString() const;
  drimysql();
  drimysql ( const drimysql& other );
  virtual ~drimysql();

protected:
  void printWarning ( const char *msg );
  void printError ( const char *msg );

private:
  int _dbdriver;
  int _gstate;
  int _gerror;
  unsigned long long _gpos;
  char *_username, *_passwd;
  char *_lastquery;
  char *_dbname;
  bool _unix_sock, _ssl;

  // MYSQL
  MYSQL *_mconnector, _mysql;
  MYSQL_RES *_mresult;
  int _mstate;
};

LIB_EXPORT DBconnectorPrivate* getDriver()
{
  return new drimysql;
}

LIB_EXPORT void removeDriver ( DBconnectorPrivate *dri )
{
  delete dri;
}

#endif // DRIMYSQL_H
